package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.object.pojo.dto.AccountSpikeDTO;
import com.tbit.uqbike.object.pojo.dto.AccountSpikeGetDTO;
import com.tbit.uqbike.object.pojo.vo.AccountSpikeVo;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AccountSpikeService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 禁行区道钉管理
 *
 * @Author LHX
 * @ClassName AccountSpikeController
 * @Description
 * @date 2023/5/12 10:39
 * @Version 1.0
 */
@RestController
@RequestMapping("/accountSpike")
public class AccountSpikeController {

    @Autowired
    private AccountSpikeService accountSpikeService;

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping("/queryPage")
    public Result<PageResult<AccountSpikeVo>> queryPage(AccountSpikeGetDTO dto) {
        PageResult<AccountSpikeVo> accountSpikeVoPageResult = accountSpikeService.queryPage(dto);
        return Result.success(accountSpikeVoPageResult);
    }


    /**
     * 修改道钉信息
     *
     * @return
     */
    @RequestMapping("/updateAccountSpike")
    public Result<String> updateAccountSpike(@RequestBody AccountSpikeDTO dto, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        if (StrUtil.isNotEmpty(dto.getSpikeNo()) && dto.getSpikeNo().length() > 20){
            return Result.error("道钉编号过长");
        }else if (StrUtil.isNotEmpty(dto.getMac()) && dto.getMac().length() > 100){
            return Result.error("MAC地址过长");
        }else if (StrUtil.isNotEmpty(dto.getAddress()) && dto.getAddress().length() > 100){
            return Result.error("地址过长");
        }
        return accountSpikeService.updateAccountSpike(dto, login);
    }

    /**
     * 删除道钉信息
     *
     * @return
     */
    @RequestMapping("/deleteAccountSpike")
    public Result<String> deleteAccountSpike(AccountSpikeDTO dto) {
        return accountSpikeService.deleteAccountSpike(dto);
    }

    /**
     * 新增道钉信息
     *
     * @param dto
     * @param request
     * @return
     */
    @RequestMapping("/addAccountSpike")
    public Result<String> addAccountSpike(@RequestBody AccountSpikeDTO dto, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        if (StrUtil.isNotEmpty(dto.getSpikeNo()) && dto.getSpikeNo().length() > 20){
            return Result.error("道钉编号过长");
        }else if (StrUtil.isNotEmpty(dto.getMac()) && dto.getMac().length() > 100){
            return Result.error("MAC地址过长");
        }else if (StrUtil.isNotEmpty(dto.getAddress()) && dto.getAddress().length() > 100){
            return Result.error("地址过长");
        }
        return accountSpikeService.addAccountSpike(dto, login);
    }

    /**
     * 道钉信息导出
     *
     * @param dto
     * @return
     */
    @RequestMapping("/exportAccountSpike")
    private Result exportAccountSpike(AccountSpikeGetDTO dto) {
        return accountSpikeService.exportAccountSpike(dto);
    }

    /**
     * 批量新增或修改
     *
     * @param file
     * @param batchType 0:新增 1：修改
     * @return
     */
    @RequestMapping("/batchAddOrUpdate")
    public Result batchAddOrUpdate(@RequestParam("file") MultipartFile file, Integer batchType, Integer accountId, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        return accountSpikeService.batchAddOrUpdate(file, batchType, accountId, login);
    }

    /**
     * 批量新增模板
     *
     * @return
     */
    @GetMapping("/getBatchAddTemplate")
    public Result getBatchAddTemplate() {
        ExcelUtil<AccountSpikeDTO> excelUtil = new ExcelUtil<>(AccountSpikeDTO.class);
        List<AccountSpikeDTO> list = new ArrayList<>();
        AccountSpikeDTO vo = new AccountSpikeDTO();
        vo.setSpikeNo("test01");
        vo.setType(1);
        vo.setMac("test01");
        vo.setState(1);
        vo.setAddress("测试地址");
        vo.setLonC(114.40407);
        vo.setLatC(30.455691);
        list.add(vo);
        return Result.success(excelUtil.exportExcel(list, "道钉信息模板").getData());
    }
}
